import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/views/layout/Layout'

/** note: Submenu only appear when children.length>=1
 *  detail see  https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 **/

/**
 * hidden: true                   if `hidden:true` will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu, whatever its child routes length
 *                                if not set alwaysShow, only more than one route under the children
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noredirect           if `redirect:noredirect` will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    perms: ['GET /aaa','POST /bbb']     will control the page perms (you can set multiple perms)
    title: 'title'               the name show in submenu and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar,
    noCache: true                if true ,the page will no be cached(default is false)
  }
 **/
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/authredirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/errorPage/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/errorPage/401'),
    hidden: true
  },
  {
    path: '',
    component: Layout,
    redirect: 'dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  }
]

export const asyncRoutes = [
  {
    path: '/complaint',
    component: Layout,
    redirect: 'noredirect',
    alwaysShow: true,
    name: 'complaintManage',
    meta: {
      title: '主诉管理',
      icon: 'el-icon-chat-line-round'
    },
    children: [{
      path: 'list',
      component: () => import('@/views/complaint/list'),
      name: 'complaintList',
      meta: {
        perms: ['GET /admin/complaint/list', 'POST /admin/complaint/delete'],
        title: '主诉管理',
        noCache: true
      }
    }, {
      path: 'create',
      component: () => import('@/views/complaint/create'),
      name: 'complaintCreate',
      meta: {
        perms: ['POST /admin/complaint/create'],
        title: '新建主诉',
        noCache: true
      }
    }, {
      path: 'edit',
      component: () => import('@/views/complaint/edit'),
      name: 'complaintEdit',
      meta: {
        perms: ['POST /admin/complaint/edit'],
        title: '编辑主诉',
        noCache: true
      },
      hidden: true
    }, {
      path: 'diagnosis',
      component: () => import('@/views/complaint/diagnosis'),
      name: 'complaintDiagnosis',
      meta: {
        perms: [
          'GET /admin/complaint/read',
          'GET /admin/complaint/diagnosis/list',
          'POST /admin/complaint/diagnosis/create',
          'GET /admin/complaint/diagnosis/read',
          'POST /admin/complaint/diagnosis/update',
          'POST /admin/complaint/diagnosis/delete'
        ],
        title: '主诉诊断',
        noCache: true
      },
      hidden: true
    }]
  },
  {
    path: '/diagnosis',
    component: Layout,
    redirect: 'noredirect',
    alwaysShow: true,
    name: 'diagnosisManage',
    meta: {
      title: '诊断管理',
      icon: 'el-icon-notebook-1'
    },
    children: [{
      path: 'list',
      component: () => import('@/views/diagnosis/list'),
      name: 'diagnosisList',
      meta: {
        perms: ['GET /admin/diagnosis/list', 'POST /admin/diagnosis/delete'],
        title: '诊断管理',
        noCache: true
      }
    }, {
      path: 'create',
      component: () => import('@/views/diagnosis/create'),
      name: 'diagnosisCreate',
      meta: {
        perms: ['POST /admin/diagnosis/create'],
        title: '新建诊断',
        noCache: true
      }
    }, {
      path: 'edit',
      component: () => import('@/views/diagnosis/edit'),
      name: 'diagnosisEdit',
      meta: {
        perms: ['POST /admin/diagnosis/edit'],
        title: '诊断编辑',
        noCache: true
      },
      hidden: true
    }, {
      path: 'drug',
      component: () => import('@/views/diagnosis/drug'),
      name: 'diagnosisDrug',
      meta: {
        perms: [
          'GET /admin/diagnosis/read',
          'GET /admin/diagnosis/drug/list',
          'POST /admin/diagnosis/drug/create',
          'GET /admin/diagnosis/drug/read',
          'POST /admin/diagnosis/drug/update',
          'POST /admin/diagnosis/drug/delete'
        ],
        title: '诊断用药',
        noCache: true
      },
      hidden: true
    }]
  },
  {
    path: '/drug',
    component: Layout,
    redirect: 'noredirect',
    alwaysShow: true,
    name: 'drugManage',
    meta: {
      title: '药品管理',
      icon: 'el-icon-coin'
    },
    children: [{
      path: 'list',
      component: () => import('@/views/drug/list'),
      name: 'drugList',
      meta: {
        perms: ['GET /admin/drug/list', 'POST /admin/drug/delete'],
        title: '药品管理',
        noCache: true
      }
    }, {
      path: 'create',
      component: () => import('@/views/drug/create'),
      name: 'drugCreate',
      meta: {
        perms: ['POST /admin/drug/create'],
        title: '新建药品',
        noCache: true
      }
    }, {
      path: 'edit',
      component: () => import('@/views/drug/edit'),
      name: 'drugEdit',
      meta: {
        perms: ['POST /admin/drug/edit'],
        title: '药品编辑',
        noCache: true
      },
      hidden: true
    }, {
      path: 'usage',
      component: () => import('@/views/drug/usage'),
      name: 'drugUsage',
      meta: {
        perms: ['GET /admin/drug/read', 'GET /admin/drug/usage/list', 'POST /admin/drug/usage/create',
          'GET /admin/drug/usage/read', 'POST /admin/drug/usage/update', 'POST /admin/drug/usage/delete',
          'GET /admin/drug/usage/listDiag','POSt /admin/drug/usage/delDiag'],
        title: '药品用法',
        noCache: true
      },
      hidden: true
    }]
  },
  {
    path: '/sys',
    component: Layout,
    redirect: 'noredirect',
    alwaysShow: true,
    name: 'sysManage',
    meta: {
      title: '系统管理',
      icon: 'lock'
    },
    children: [
      {
        path: 'admin',
        component: () => import('@/views/sys/admin'),
        name: 'admin',
        meta: {
          perms: ['GET /admin/admin/list', 'POST /admin/admin/create', 'POST /admin/admin/update', 'POST /admin/admin/delete'],
          title: '管理员',
          noCache: true
        }
      },
      {
        path: 'notice',
        component: () => import('@/views/sys/notice'),
        name: 'sysNotice',
        meta: {
          perms: ['GET /admin/notice/list', 'POST /admin/notice/create', 'POST /admin/notice/update', 'POST /admin/notice/delete'],
          title: '通知管理',
          noCache: true
        }
      },
      {
        path: 'log',
        component: () => import('@/views/sys/log'),
        name: 'log',
        meta: {
          perms: ['GET /admin/log/list'],
          title: '操作日志',
          noCache: true
        }
      },
      {
        path: 'role',
        component: () => import('@/views/sys/role'),
        name: 'role',
        meta: {
          perms: ['GET /admin/role/list', 'POST /admin/role/create', 'POST /admin/role/update', 'POST /admin/role/delete', 'GET /admin/role/permissions', 'POST /admin/role/permissions'],
          title: '角色管理',
          noCache: true
        }
      },
      {
        path: 'os',
        component: () => import('@/views/sys/os'),
        name: 'os',
        meta: {
          perms: ['GET /admin/storage/list', 'POST /admin/storage/create', 'POST /admin/storage/update', 'POST /admin/storage/delete'],
          title: '对象存储',
          noCache: true
        }
      }
    ]
  },

  {
    path: '/profile',
    component: Layout,
    redirect: 'noredirect',
    alwaysShow: true,
    children: [
      {
        path: 'password',
        component: () => import('@/views/profile/password'),
        name: 'password',
        meta: { title: '修改密码', noCache: true }
      },
      {
        path: 'notice',
        component: () => import('@/views/profile/notice'),
        name: 'notice',
        meta: { title: '通知中心', noCache: true }
      }
    ],
    hidden: true
  },

  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
